package com.ruoyi.project.hcy.task.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.administrations.domain.Crew;
import com.ruoyi.project.hcy.task.domain.Task;
import com.ruoyi.project.hcy.task.domain.TaskTransfer;
import com.ruoyi.project.hcy.task.service.ITaskService;
import com.ruoyi.project.hcy.task.service.ITaskTransferService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务转移操作管理
 */
@Controller
@RequestMapping("/hcy/task/transfer")
public class TaskTransferController extends BaseController {

    @Autowired
    ITaskService taskService;

    @Autowired
    ITaskTransferService taskTransferService;

    private String prefix = "hcy/task/tasktransfer";

    @RequiresPermissions("hcy:postpone:view")
    @GetMapping()
    public String transfer() {

        return prefix + "/transfer";

    }

    @RequiresPermissions("hcy:transfer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskTransfer taskTransfer) {
        startPage();
        List<TaskTransfer> list = taskTransferService.selectTaskTransferList(taskTransfer);
        return getDataTable(list);
    }

    @RequiresPermissions("hcy:transfer:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";

    }

    @RequiresPermissions("hcy:transfer:add")
    @Log(title = "任务延期管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TaskTransfer taskTransfer, Long taskId, Long userId, Long executorId) {

        return toAjax(taskTransferService.insertTransfer(taskTransfer, taskId, userId));
    }

    @RequiresPermissions("hcy:transfer:add")
    @PostMapping("/crewOfTask")
    @ResponseBody
    public Crew crewOfTask(Long taskId) {
        return taskTransferService.selectCrewOfTask(taskId);
    }

    @RequiresPermissions("hcy:transfer:add")
    @PostMapping("/taskList")
    @ResponseBody
    public List<Task> taskList() {
        return taskTransferService.selectTaskList();
    }

    @RequiresPermissions("hcy:transfer:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(taskTransferService.deleteTaskTransferByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{transferId}")
    public String edit(@PathVariable("transferId") Long transferId, ModelMap mmap) {
        mmap.put("taskTransfer", taskTransferService.selectTaskTransferById(transferId));
        return prefix + "/edit";
    }

    @RequiresPermissions("hcy:transfer:edit")
    @Log(title = "转移申请管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskTransfer taskTransfer) {

        return toAjax(taskTransferService.updateTaskTransfer(taskTransfer));
    }

    @RequiresPermissions("hcy:transfer:verifyview")
    @GetMapping("/verifyview")
    public String verify() {

        return prefix + "/verifyview";

    }

    @GetMapping("/verify/{transferId}")
    public String verify(@PathVariable("transferId") Long transferId, ModelMap mmap) {
        TaskTransfer taskTransfer = taskTransferService.selectTaskTransferById(transferId);
        mmap.put("taskTransfer", taskTransfer);
        mmap.put("task", taskTransfer.getTask());
        mmap.put("crews", taskTransferService.selectCrews());
        return prefix + "/verify";
    }

    @RequiresPermissions("hcy:transfer:verify")
    @Log(title = "转移申请批复", businessType = BusinessType.UPDATE)
    @PostMapping("/verify")
    @ResponseBody
    public AjaxResult verifySave(@Validated TaskTransfer taskTransfer, Long crewId, Long taskId) {

        return toAjax(taskTransferService.verifyTaskTransfer(taskTransfer, crewId, taskId));
    }


}
